/**
 * @param {string} s
 * @param {string} locked
 * @return {boolean}
 */
 var canBeValid = function(s, locked) {
  const n = s.length
  if(n % 2 !== 0) return false
  const check = (str) => {
    let stack = []
    for(let ch of str){
      if(ch === '('){
        stack.push(ch)
      }else{
        if(stack.length === 0) return false
        stack.pop()
      }
    }
    return stack.length === 0
  }
  let arr = []
  for(let i = 0; i<n; i++){
    if(locked[i] === '1') arr.push([i, s[i]])
  }
  if(arr.length > 0){
    if(arr[0][0] === 0 && arr[0][1] === ')') return false
    if(arr[arr.length-1][0] === n-1 && arr[arr.length-1][1] ==='(') return false
  }
  let cnt1 = 0, cnt2 = 0
  for(let [k, v] of arr){
    if(v === '(') cnt1++
    else cnt2++
  }
  if(Math.max(cnt1, cnt2) * 2 > n) return false
  // check  (
  let l = 0
  while(l < arr.length && arr[l][1] === ')') l++
  if(l < arr.length && n - arr[l][0] - 1<2*cnt1) return false
  // check )
  let r = arr.length-1
  while(r >= 0 && arr[r][1] === '(') r--
  if(r > 0 && arr[r][0] < 2*cnt2) return false
  return true
};